package net.toocruel.iqismart.entity.core;

import net.toocruel.iqismart.entity.base.BaseEntity;
import net.toocruel.iqismart.entity.constants.Action;
import net.toocruel.iqismart.entity.constants.SourceType;
import net.toocruel.iqismart.entity.core.Answer;
import net.toocruel.iqismart.entity.core.Article;
import net.toocruel.iqismart.entity.core.Question;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.entity.setting.Setting;

import javax.persistence.*;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/8 上午11:35
 */
@Entity
public class Credit extends BaseEntity {
    private static final long serialVersionUID = -4650381804633478622L;
    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY,optional = false)
    @JoinColumn(name = "userId")
    private User user;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "questionId")
    private Question question;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "answerId")
    private Answer answer;

    @ManyToOne(cascade = CascadeType.DETACH,fetch = FetchType.LAZY)
    @JoinColumn(name = "articleId")
    private Article article;

    @Enumerated(EnumType.ORDINAL)
    private Action action;

    private int coin = 0;
    private int credit = 0;

    public Credit() {
    }

    public Credit(User user,Action action) {
        init(user,action);
    }

    private void init(User user,Action action) {
        if(action == Action.SIGN){
            coin = Setting.SETTING.getCoin().getLogin();
            credit = Setting.SETTING.getCredit().getLogin();
        }else if(action == Action.ANSWER_ADOPTED){
            coin = Setting.SETTING.getCoin().getAdopted();
            credit = Setting.SETTING.getCredit().getAdopted();
        }else if(action == Action.CREATE_QUESTION){
            coin = Setting.SETTING.getCoin().getAsk();
            credit = Setting.SETTING.getCredit().getAsk();
        }else if(action == Action.CREATE_ANSWER){
            coin = Setting.SETTING.getCoin().getAnswer();
            credit = Setting.SETTING.getCredit().getAnswer();
        }else if(action == Action.CREATE_ARTICLE){
            coin = Setting.SETTING.getCoin().getWriteArticle();
            credit = Setting.SETTING.getCredit().getWriteArticle();
        }
        this.user = user;
        this.action = action;
    }

    public Credit(User user,Action action,Question question, Answer answer) {
        init(user,action);
        this.question = question;
        this.answer = answer;
    }

    public Credit(User user,Action action, Question question) {
        init(user,action);
        this.question = question;
    }


    public Credit(User user,Action action, Article article) {
        init(user,action);
        this.article = article;
    }




    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public Answer getAnswer() {
        return answer;
    }

    public void setAnswer(Answer answer) {
        this.answer = answer;
    }

    public Article getArticle() {
        return article;
    }

    public void setArticle(Article article) {
        this.article = article;
    }

    public Action getAction() {
        return action;
    }

    public void setAction(Action action) {
        this.action = action;
    }

    public int getCoin() {
        return coin;
    }

    public void setCoin(int coin) {
        this.coin = coin;
    }

    public int getCredit() {
        return credit;
    }

    public void setCredit(int credit) {
        this.credit = credit;
    }
}
